<template>
  <div
    class="node-item"
    ref="nodeItemRef"
    :class="[isActive || isSelected ? 'active' : '']"
    :style="nodeCoordinate"
    v-click-outside="setNotActive"
    @click="setActive"
    @mouseenter="showAnchor"
    @mouseleave="hideAnchor"
    @contextmenu.prevent="onContextmenu"
  >
    <div class="node-header" :style="setHeaderColor(node.type)">
      <span class="span-ellipsis">
        {{ node.sourceTableEnName }}({{ node.sourceTableCnName }})</span
      >
      <!--连线用--//触发连线的区域-->
      <div class="node-anchor anchor-top" v-show="mouseEnter"></div>
      <div class="node-anchor anchor-right" v-show="mouseEnter"></div>
      <div class="node-anchor anchor-bottom" v-show="mouseEnter"></div>
      <div class="node-anchor anchor-left" v-show="mouseEnter"></div>
    </div>

    <transition name="fade">
      <div id="show" v-if="show">
        <div class="select-fields">
          <div class="content">
            <span>已选字段(21)</span>
            <i class="el-icon-setting pointer"></i>
          </div>
        </div>
        <el-input
          placeholder="请输入"
          suffix-icon="el-icon-search"
          v-model="formData['name']"
          size="small"
          @input="handleInput"
        >
        </el-input>
        <ul :id="`${node.id}${minus}cols`" class="cols">
          <li
            class="col"
            v-for="col in node.columns.filter(
              (data) => !formData.name || data.name.includes(formData.name)
            )"
            :key="`${node.sourceTableEnName}${minus}${col.name}`"
            :id="`${node.sourceTableEnName}${minus}${col.name}`"
            @mouseover="handleMouseover(node, col)"
            @mouseout="handleMouseout(node, col)"
          >
            <span>{{ col.name }}</span>
            <i class="el-icon-setting pointer"></i>
          </li>
        </ul>
        <noData v-if="showNoData" height="auto"></noData>
      </div>
    </transition>
  </div>
</template>

<script>
import { tbType } from "../config/tbType";
import noData from "@/components/common/noData";
import ClickOutside from "vue-click-outside";
import $ from "jquery";

export default {
  name: "NodeItem",
  components: { noData },
  props: {
    node: Object,
    jsPlumb: {
      default: null,
    },
  },
  directives: {
    ClickOutside,
  },
  data() {
    return {
      tbType: tbType,
      minus: "-", // 表名和列名的分割符号
      formData: {},
      showNoData: false,
      show: true,
      mouseEnter: false,
      isActive: false,
      isSelected: false,
    };
  },
  methods: {
    showAnchor() {
      this.mouseEnter = true;
    },
    hideAnchor() {
      this.mouseEnter = false;
    },
    // 右键删除
    onContextmenu() {
      this.$contextmenu({
        items: [
          {
            label: "删除",
            disabled: false,
            icon: "",
            onClick: () => {
              this.$confirm("是否删除此数据?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
              })
                .then(() => {
                  this.deleteNode();
                })
                .catch(() => {
                  this.$message.info("已取消");
                });
            },
          },
        ],
        event,
        customClass: "custom-class",
        zIndex: 9999,
        minWidth: 180,
      });
    },
    setActive() {
      if (window.event.ctrlKey) {
        this.isSelected = !this.isSelected;
        return false;
      }
      this.isActive = true;
      this.isSelected = false;
      setTimeout(() => {
        this.$emit("changeLineState", this.node.id, true);
      }, 0);
    },
    // vue 自定义指令clickoutside实现点击其他元素才会触发
    // 用户获取节点的ID值
    setNotActive() {
      if (!window.event.ctrlKey) {
        this.isSelected = false;
      }
      if (!this.isActive) {
        return;
      }
      this.$emit("changeLineState", this.node.id, false);
      this.isActive = false;
    },
    deleteNode() {
      this.$emit("deleteNode", this.node);
    },
    setHeaderColor(type) {
      for (let key in tbType) {
        if (type === key) {
          return {
            backgroundColor: tbType[key].color,
          };
        }
      }
    },
    // 过滤数据
    handleInput() {
      this.mouseEnter = false;
      let columns = this.node.columns.filter(
        (data) => !this.formData.name || data.name.includes(this.formData.name)
      );
      this.showNoData = !columns.length;
    },
    handleMouseover(node, col) {
      let p = `${node.id}${this.minus}cols`,
        p1 = `${node.sourceTableEnName}${this.minus}${col.name}`;

      $("#" + p)
        .find("#" + p1)
        .css("background-color", tbType["HighLight"].color);
    },
    handleMouseout(node, col) {
      let p = `${node.id}${this.minus}cols`,
        p1 = `${node.sourceTableEnName}${this.minus}${col.name}`;

      $("#" + p)
        .find("#" + p1)
        .css("background-color", tbType["NormalLight"].color);
    },
  },
  computed: {
    nodeCoordinate: {
      get() {
        return {
          top: this.node.top,
          left: this.node.left,
        };
      },
    },
  },
};
</script>

<style lang="less" scoped>
@labelColor: #409eff;
@nodeSize: 20px;
@viewSize: 10px;
.node-item {
  position: absolute;
  // display: felx;
  // cursor: move;
  border: 1px solid #b7b6b6;
  // border-top-left-radius: 3px;
  // border-top-right-radius: 3px;
  justify-items: center;
  align-items: center;
  z-index: 9995;
  width: 240px;
  // .title {
  //   text-align: left;
  //   padding: 0 12px;
  //   cursor: move;
  // }
  .node-header {
    // border-top-left-radius: 3px;
    // border-top-right-radius: 3px;
    // position: absolute;
    // width: 200px;
    padding: 10px;
    background-color: #91c051;
    color: white;
    font-size: 12px;
    display: flex;
    cursor: move;
    // position: relative;

    .node-anchor {
      display: flex;
      position: absolute;
      width: @nodeSize;
      height: @nodeSize;
      align-items: center;
      justify-content: center;
      border-radius: 10px;
      cursor: crosshair;
      z-index: 9999;
      background: -webkit-radial-gradient(
        sandybrown 10%,
        white 30%,
        #9a54ff 60%
      );
    }
    .anchor-top {
      top: calc((@nodeSize / 2) * -1);
      left: 50%;
      margin-left: calc((@nodeSize / 2) * -1);
    }
    .anchor-right {
      top: 50%;
      right: calc((@nodeSize / 2) * -1);
      margin-top: calc((@nodeSize / 2) * -1);
    }
    .anchor-bottom {
      bottom: calc((@nodeSize / 2) * -1);
      left: 50%;
      margin-left: calc((@nodeSize / 2) * -1);
    }
    .anchor-left {
      top: 50%;
      left: calc((@nodeSize / 2) * -1);
      margin-top: calc((@nodeSize / 2) * -1);
    }
  }
  .active {
    border: 1px dashed @labelColor;
    box-shadow: 0px 5px 9px 0px rgba(0, 0, 0, 0.5);
  }
  .col {
    list-style: none;
    background-color: #fff;
    padding: 8px 10px;
    font-size: 12px;
    width: auto;
    /*行内文本太长后面的就省略*/
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    display: flex;
    justify-content: space-between;
    cursor: default;
    // &:hover{
    //   background-color: #c1e4e9;
    // }
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.cols {
  max-height: 200px;
  overflow-y: auto;
}

.select-fields {
  background-color: #f5f7fa;
  cursor: default;
  .content {
    padding: 8px 10px;
    font-size: 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
